<?php

namespace org\phpstruct\util;

interface NavigationErrors {
    const NOT_FOUND = 404;
    const SERVER_ERROR = 500;
    const ACCESS_DENIED = 403;
}

/**
 * Description of Navigator
 *
 * @author Emanuele Colonnelli <emanuele.colonnelli@php-struct.org>
 */
class Navigator implements NavigationErrors {

    private static function handleError($url) {
        if ($headers_sent) {
            die("<script type='text/javascript'>document.location.replace('$url');</script>");
        } else {
            header("Location:$url");
        }
    }

    public static function errorPage($code = null) {

        $headers_sent = \headers_sent();

        if (!\is_null($code)) {

            switch ($code) {
                case self::ACCESS_DENIED:
                case self::NOT_FOUND:
                case self::SERVER_ERROR:
                    $str = sprintf("error.page_%s", self::NOT_FOUND);
                    $url = \org\phpstruct\core\Config::get($str);
                    self::handleError($url);
                    break;
                default:
                    die("Sorry");
                    break;
            }
        } else {

            die("Sorry!");
        }
    }

}
